<?php

namespace Lib;

class Session {

	/**
	 * 初始化SESSION
	 *
	 * @param 配置 $aConf
	 *        	disable	禁用SESSION
	 *        	enable	启用SESSION
	 *        	lifetime SESSION生命周期
	 *        	domain	域名
	 *        	method	提交方式POST或GET默认使用COOKIE
	 *        	name	传递时的变量名默认sid
	 *        	handler	储存方式文件、MEMCACHED、SQLITE，默认为文件
	 *        	server	使用memcached时的服务器可以为数组或用逗号分隔使用多台服务器
	 *        	file	使用sqlite时的数据库文件
	 * @return boolean
	 */
	public function init($aConf = null) {
		global $_CONFIG;
		if ($aConf == null) {
			$aConf = $_CONFIG ['session'];
		} else {
			$aConf += ( array ) $_CONFIG ['session'];
		}
		
		if ($aConf ['disable'] || ! $aConf ['enable'] || $_CONFIG ['base'] ['http'] == false) {
			return true;
		}
		
		$iLifetime = $aConf ['lifetime'] ? $aConf ['lifetime'] : 0;
		$sPath = $_CONFIG ['base'] ['path'];
		$sDomain = $_SERVER ['SERVER_PORT'] == 443 ? 'https' : 'http' . '://' . $aConf ['domain'] ? $aConf ['domain'] : $_CONFIG ['base'] ['host'];
		$bSecure = $_SERVER ['SERVER_PORT'] == 443 ? true : false;
		session_set_cookie_params ( $iLifetime, $sPath, $sDomain, $bSecure, true );
		$sMethod = $aConf ['method'];
		$sName = $aConf ['name'] ? $aConf ['name'] : 'sid';
		session_name ( $sName );
		if ($sMethod && $sName) {
			if ($sMethod == 'post' && $_POST [$sName]) {
				session_id ( $_POST [$sName] );
			} elseif ($sMethod == 'get' && $_GET [$sName]) {
				session_id ( $_GET [$sName] );
			}
		}
		if ($aConf ['handler'] == 'memcached') {
			$this->_setMemcached ( $aConf ['server'] );
		} elseif ($aConf ['handler'] == 'sqlite') {
			$this->_setSqlite ( $aConf ['file'] );
		}
		if ($aConf ['name']) {
			ini_set ( 'session.name', $aConf ['name'] );
		}
		session_start ();
		if ($sMethod && $sName) {
			if ($sMethod == 'post') {
				$_POST [$sName] = session_id ();
			} elseif ($sMethod == 'get') {
				$_GET [$sName] = session_id ();
			}
		}
		return true;
	}

	public function get($sName) {
		if (strpos ( $sName, '.' )) {
			$aName = explode ( '.', $sName );
			$sValue = $_SESSION;
			foreach ( $aName as $v ) {
				$sValue = $sValue [$v];
				if (! is_array ( $sValue )) {
					return $sValue;
				}
			}
		} else {
			return $_SESSION [$sName];
		}
	}

	public function set($sName, $sValue) {
		if (strpos ( $sName, '.' )) {
			$aName = explode ( '.', $sName );
			$sValue = $_SESSION;
			foreach ( $aName as $v ) {
				$sValue = $sValue [$v];
				if (! is_array ( $sValue )) {
					return $sValue;
				}
			}
		} else {
			return $_SESSION [$sName] = $sValue;
		}
	}

	private function _setMemcached($aServer) {
		ini_set ( 'session.save_handler', 'memcached' );
		if (is_array ( $aServer )) {
			ini_set ( 'session.save_path', implode ( ',', $aServer ) );
		} else {
			ini_set ( 'session.save_path', $aServer );
		}
	}

	private function _setSqlite($sFile) {
		if (is_file ( ROOT . '/' . $sFile )) {
			ini_set ( 'session.save_handler', 'sqlite' );
			ini_set ( 'session.save_path', ROOT . '/' . $sFile );
			return true;
		} else {
			return false;
		}
	}

	public function __get($sName) {
		return $_SESSION [$sName];
	}

	public function __set($sName, $sValue) {
		return $_SESSION [$sName] = $sValue;
	}
}